﻿ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
 
using System.Web;
using System.Web.Security;
 
using MySql.Data.MySqlClient;
 
using System.Data;
using System.Configuration;
using LapvComponent;
 

namespace LapvBussiness
{
    
    public class ProviderAdmin
    {
        public string domain = "";//ConfigurationManager.AppSettings["DomainNews"].ToString();
        #region AUTHEN
        public static int GetUsserId()
        {
            int userid = 0;
            if (HttpContext.Current.User.Identity.IsAuthenticated == false) return 0;
            else
            {
                if(HttpContext.Current.Session["UserId"] == null)
                {
                    string UserName = HttpContext.Current.User.Identity.Name;
                    account acc = null;
                    //using (lapvEntities db = new lapvEntities())
                    //{
                    //     acc = db.accounts.Include("roles").Where(en => en.username.ToLower() == UserName.ToLower()).FirstOrDefault();
                    //}
                    if (acc != null)
                    {
                        userid = acc.id;

                        HttpContext.Current.Session["UserId"] = acc.id;
                        HttpContext.Current.Session["UserName"] = acc.username;
                  //      HttpContext.Current.Session["Role"] = acc.role.id;
                    }
                }
                else
                {
                    int.TryParse(HttpContext.Current.Session["UserId"].ToString(), out userid);
                }
                return userid;
            }
        }

        public static void CheckAuthen(string returnUrl, int permission)
        {
            //string domain = ConfigLapv.Instance.DomainAdmin;
            
            
            //    int userid = GetUsserId();
            //    if (userid <= 0)
            //    {
            //        HttpContext.Current.Response.Cookies[FormsAuthentication.FormsCookieName].Expires = DateTime.Now.AddDays(-1);
            //        HttpContext.Current.Response.Redirect(domain + "Login.aspx?returnurl=" + returnUrl);
            //    }
            //    else
            //    {
            //        List<int> types = new List<int>();
            //        if (HttpContext.Current.Session["ListPer"] == null)
            //        {
            //            if (HttpContext.Current.Session["Role"] != null)
            //            {
            //                int role = int.Parse(HttpContext.Current.Session["Role"].ToString());
            //                if (role > 0)
            //                {
            //                    List<role_permission> lstPers = new List<role_permission>();
            //                    using (lapvEntities db = new lapvEntities())
            //                    {
            //                        account gm = null;// db.accounts.Where(ec => ec.id == userid).FirstOrDefault();
            //                        if (gm != null)
            //                        {
            //                            string ips = gm.ipcanlogin;
            //                            string appid = HttpContext.Current.Request.UserHostName;
            //                            if ((!ips.Contains("," + appid + ",")) && ips != "*")
            //                            {
            //                                HttpContext.Current.Response.Redirect(domain + "default.aspx", false);
            //                            }
            //                            else {
            //                              lstPers = db.role_permission.Include("role").Include("permission").Where(en => en.role.id == role).ToList();
            //                             }
            //                        }
            //                        else HttpContext.Current.Response.Redirect(domain + "default.aspx", false);
            //                    }
            //                    if (lstPers != null && lstPers.Count() > 0)
            //                    {
            //                        foreach (role_permission per in lstPers)
            //                        {
            //                            types.Add(per.permission.type);
            //                            HttpContext.Current.Session["ListPer"] = types;
            //                        }
            //                    }
            //                }
            //            }
            //            else//not login
            //            { }
            //        }
            //        else
            //        {
            //            types = (List<int>)HttpContext.Current.Session["ListPer"];
            //        }
            //        if (!types.Contains(permission))
            //            HttpContext.Current.Response.Redirect(domain+ "default.aspx", false);
            //    }
            
        }
        #endregion
        #region LOG
        public static void AddLog(int userid, string username, string _log, string _log2, string type)
        {
            try
            {
                //using (lapvEntities db = new lapvEntities())
                //{
                //    log newlog = new log();
                //    newlog.date = DateTime.Now;
                //    newlog.gmId = userid;
                //    newlog.username = username;
                //    newlog.ip = HttpContext.Current.Request.UserHostName;
                //    newlog.logcontent = _log;
                //    newlog.logcontent2 = _log2;
                //    newlog.type = type;
                //  //  db.AddTolog(newlog);
                //    db.SaveChanges();
                //}
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void DeleteOldAdminLog(DateTime fromdate)
        {
        //    try
        //    {
        //        string strconn = ConfigurationManager.ConnectionStrings["EducationManagerConnection"].ConnectionString; ;
        //        Connection connect = new Connection(strconn);
        //        connect.executeNonQuery("WA_DeleteOldLog", new MySqlParameter[] { new MySqlParameter("fromdate", fromdate) });
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
            //}
        }

        public static void DeleteOldAccountLog(DateTime fromdate)
        {
            //try
            //{
            //    connection connect = new connection();
            //    connect.executenonquery("wa_deleteoldaccountlog", new mysqlparameter[] { new mysqlparameter("fromdate", fromdate) });
            //}
            //catch (exception ex)
            //{
            //    throw ex;
            //}
        }
        #endregion
    }
}
